package com.xy.sdk.share;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;

import android.util.Log;
import android.widget.Toast;


import java.io.File;

import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_QQ;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_QQ_YS;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_QZ;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_QZ_YS;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_SAVE_IV;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_WB;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_WB_YS;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_WX;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_WXPY;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_WXPY_YS;
import static com.xy.sdk.share.ShareConstants.SHARE_TYPE_WX_YS;

//import androidx.core.app.ActivityCompat;
//import androidx.core.content.ContextCompat;

/**
 * Craeted by tianshen
 * Created on 2021/4/19
 */
public class ShareSDK {

    private static ShareSDK instance;
    public static final String SDCARD_ROOT = Environment.getExternalStorageDirectory().getAbsolutePath();


    private ShareSDK() {
    }

    public static ShareSDK instance() {
        if (instance == null) {
            instance = new ShareSDK();
        }
        return instance;
    }

    public void shareParms(Bundle bundle) {
    }



    /**
     * 分享类型
     *
     * @param context
     * @param path          图片路径
     * @param shareType     分享类型
     * @param shareCallback 分享回调
     */
    public void share(Activity context, String path, int shareType, ShareCallback shareCallback) {
        File file = new File(path);
        if (!file.exists()) {
            String tip = "文件不存在";
            Toast.makeText(context, tip + " path = " + path, Toast.LENGTH_LONG).show();
            return;
        }
        Log.e("XYSDK", "share:" + path);

//        int permissionCheck = context.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int permissionCheck = context.checkSelfPermission( Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(context,
            context.requestPermissions(
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE},
                    ShareConstants.PERMISSIONS_REQUEST_STORAGE);
            return;
        }

        switch (shareType) {
            case SHARE_TYPE_QQ_YS:
            case SHARE_TYPE_QZ_YS:
            case SHARE_TYPE_QQ:
            case SHARE_TYPE_QZ:
                NativeShare.shareImageToQQ(context, path, shareCallback);
                break;
            case SHARE_TYPE_WX_YS:
            case SHARE_TYPE_WXPY_YS:
            case SHARE_TYPE_WX:
            case SHARE_TYPE_WXPY:
                NativeShare.shareWX(context, path, shareCallback);
                break;
            case SHARE_TYPE_WB_YS:
            case SHARE_TYPE_WB:
                NativeShare.shareToWeibo(context, path, shareCallback);
                break;
            case SHARE_TYPE_SAVE_IV:
                NativeShare.saveImageToGallery(context, path);
                break;
            default:
                break;

        }

    }



    public void onRequestPermissionsResult(Activity activity, int requestCode, String permissions[],
                                           int[] grantResults) {
        switch (requestCode) {
            case ShareConstants.PERMISSIONS_REQUEST_STORAGE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else {
                    Toast.makeText(activity, "请进入 软件设置 -> 权限管理 中开启存储 权限", Toast.LENGTH_LONG).show();
                }
                return;
            }
        }

    }


}
